package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.Brand;
import com.ybkj.daijia.server.model.BrandType;
import com.ybkj.daijia.server.service.BrandService;
import com.ybkj.daijia.server.service.BrandTypeService;
import com.ybkj.daijia.server.service.CarService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "brandType")
public class BrandTypeController {

    @Autowired
    private BrandTypeService brandTypeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CarService carService;

    @RequestMapping(value = "toAdd", method = RequestMethod.GET)
    public ModelAndView add0(HttpSession session) {

        ModelAndView mv = new ModelAndView();
        mv.addObject("brandType", new BrandType());
        mv.addObject("brandName", "");

        mv.setViewName("brandType/add");
        return mv;
    }

    @RequestMapping(value = "toAdd/{id}", method = RequestMethod.GET)
    public ModelAndView add(HttpSession session, @PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView();
        Brand brand = brandService.findById(id);
        if (null != brand) {
            mv.addObject("brandName", brand.getName());
        } else {
            mv.addObject("brandName", "");
        }
        mv.addObject("brandType", new BrandType());

        mv.setViewName("brandType/add");
        return mv;
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> create(Long brandId, String name, Integer sequence) {

        Subject subject = SecurityUtils.getSubject();
        Map<String, Object> map = new HashMap<String, Object>();
        if (subject.isPermitted("brand")) {
            Brand brand = brandService.findByBrandId(brandId);
            if (brand == null) {
                map.put("success", false);
                map.put("message", "此品牌不存在!请先添加品牌");
                return map;
            }
            List<BrandType> brandTypes = brandTypeService.findByBrandId(brand.getId());

            for (BrandType t : brandTypes) {
                if (name.equals(t.getName())) {
                    map.put("success", false);
                    map.put("message", "此品牌型号已存在!!");
                    return map;
                }
            }
            BrandType brandType = new BrandType();
            brandType.setName(name);
            brandType.setSequence(sequence);
            brandType.setCreated(new Date());
            brandType.setUpdated(new Date());
            brandType.setBrandId(brand.getId());
            int i = brandTypeService.save(brandType);
            if (i == 1) {
                map.put("success", true);
                map.put("message", "保存成功");
                return map;
            } else {
                map.put("success", false);
                map.put("message", "保存失败");
                return map;
            }
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView toEdit(@PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView();
        BrandType brandType = brandTypeService.findById(id);

        Brand brand = brandService.findById(brandType.getBrandId());

        mv.addObject("brandType", brandType);
        mv.addObject("brand", brand);

        mv.setViewName("brandType/edit");
        return mv;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> update(Long brandTypeId, String brandTypeName, String brandName,
        Long brandId, Integer sequence) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {
            Map<String, Object> map = new HashMap<String, Object>();
            BrandType brandType = brandTypeService.findById(brandTypeId);
            Brand b = brandService.findById(brandId);
            if (brandType != null && brandType.getId() != brandTypeId) {
                if (b.getName() != brandName) {
                    brandType.setBrandId(b.getId());
                    brandType.setUpdated(new Date());
                    brandTypeService.update(brandType);
                    map.put("success", false);
                    map.put("message", "此品牌型号已经存在!");
                    return map;
                }
                map.put("success", false);
                map.put("message", "此品牌型号已经存在!");
                return map;
            }
            brandType.setName(brandTypeName);
            brandType.setSequence(sequence);
            brandType.setUpdated(new Date());
            brandType.setBrandId(b.getId());

            int i = brandTypeService.update(brandType);
            if (i == 1) {
                map.put("success", true);
                map.put("message", "保存成功");
            } else {
                map.put("success", false);
                map.put("message", "保存失败");
            }
            return map;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> delete(Long id) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {
            Map<String, String> result = new HashMap<String, String>();
            carService.deleteBrandType(id);        //删除关联汽车的品牌型号
            int i = brandTypeService.delete(id);
            if (i != 0) {
                result.put("success", "true");
            } else {
                result.put("success", "false");
            }

            return result;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "getBrand", method = RequestMethod.GET)
    public @ResponseBody
    List<Map<String, Object>> getCar(String brandName) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {
            List<Brand> brand = brandService.findByBrand(brandName);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (Brand b : brand) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", b.getName());
                list.add(map);
            }
            return list;
        } else {
            throw new NoPermissionException();
        }

    }

}
